package com.exampl.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.exampl.dao.RecordDao;
import com.exampl.domain.Record;
import com.exampl.service.RecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;

@Service
public class RecordServiceImpl extends ServiceImpl<RecordDao, Record> implements RecordService {

    @Autowired
    HttpSession session;

    @Override
    public void store(String name) {
        // 获取用户id
        Long user_id = (Long) session.getAttribute("user");
        LambdaQueryWrapper<Record> lqw = new LambdaQueryWrapper<Record>();
        lqw.eq(Record::getUserId, user_id);
        Record record = this.getOne(lqw);
        // 获取历史记录
        String history = record.getHistory();
        history = history.substring(1, history.length() - 1);
        int len = history.length();
        name = "\"" + name + "\"";
        String[] split = history.split(",");
        history = "[" + name + ",";
        if(len > 0){
            for(String s:split){
                if(!s.equals(name)){
                    history = history + s + ",";
                }
            }
        }
        history = history.substring(0, history.length() - 1);
        record.setHistory(history + "]");
        this.updateById(record);
    }
}
